package org.jackysoft.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jackysoft.controller.base.AbstractController;
import org.jackysoft.entity.ActionNode;
import org.jackysoft.entity.OsTask;
import org.jackysoft.entity.OsVariable;
import org.jackysoft.entity.User;
import org.jackysoft.service.OsVariableService;
import org.jackysoft.service.OsWorkflowRunnerService;
import org.jackysoft.service.WorkflowNodeService;
import org.jackysoft.util.WorkflowConstant;
import org.jackysoft.workflow.ActionItem;
import org.jackysoft.workflow.StepOwner;
import org.jackysoft.workflow.Workflow;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Maps;

/*******************************************************************************
 * 执行工作流的步骤
 * 
 * @author 屈甲康
 * @QQ 285799123
 * 
 */

@Controller
@RequestMapping("/oswf/proceed")
public class OsProceedController extends AbstractController<OsTask> {
	
	@Resource
	OsVariableService variableService;
	@Resource
	WorkflowNodeService nodeService;
	
	@Resource
	OsWorkflowRunnerService workflowService;
	
	@Resource
	Workflow workflow;
	
	@RequestMapping("/transit/{refid}/{actionId}/{auto}")
	public String proceed(
			@PathVariable("refid")long workflowId,
			@PathVariable("actionId")int actionId,
			@PathVariable("auto") boolean auto,
			Collection<StepOwner> nextOwners) throws Exception {
		String wfname = Workflow.WORKFLOW_NAME.get();

		User user = null;
		//TODO 获取当前登陆用户
		Map<String, Object> input = new HashMap<String, Object>();

		OsVariable form = variableService.findOneByName(workflowId,WorkflowConstant.FORM_TITLE.toString());
		input.put(WorkflowConstant.FORM_TITLE.toString(), form == null ? wfname
				: form.getValue());
		input.put(WorkflowConstant.SENDER.toString(), user.getLocalName());
		input.put(WorkflowConstant.SENDER_USERID.toString(), user.getUsername());
		if (actionId != -1) {
			workflow.doAction(workflowId, actionId, input, nextOwners);

		}

		ActionNode actor = nodeService.getActionNode(actionId,wfname);

		variableService.setVariable(workflowId,
				WorkflowConstant.ACTION_ID.toString(), "" + actionId);

		variableService.setVariable(workflowId, WorkflowConstant.ACTION_NAME
				.toString(), actor == null ? "" : actor.getName());

		variableService.setVariable(workflowId,
				WorkflowConstant.SEND_DATE.toString(), DateTime.now().getMillis()+"");

		variableService.setVariable(workflowId,
				WorkflowConstant.LAST_EXECUTOR.toString(), user.getLocalName());
		variableService.setVariable(workflowId,
				WorkflowConstant.SENDER.toString(), user.getLocalName());
		variableService.setVariable(workflowId,
				WorkflowConstant.SENDER_USERID.toString(), user.getUsername());
		variableService.setVariable(workflowId,
				WorkflowConstant.LAST_EXECUTOR_USERID.toString(),
				user.getUsername());
		Workflow.LOCAL_STATUS.set(WorkflowConstant.STATE_UNDERWAY);		
		return !auto ? "proceed" : "auto";
	}

	
	@RequestMapping("/prework/{refid}/{actionId}")
	public String prework(
			@ModelAttribute@PathVariable("refid")long workflowId,
			@ModelAttribute@PathVariable("actionId")int actionId,
			@ModelAttribute String actionName
			) throws Exception {
		OsVariable actionNameVar = variableService.findOneByName(workflowId,
				WorkflowConstant.ACTION_NAME.toString());
		OsVariable actionIdvar = variableService.findOneByName(workflowId,
				WorkflowConstant.ACTION_ID.toString());
		if (actionNameVar != null && actionIdvar != null) {
			actionId = Integer.parseInt(actionIdvar.getValue());
			actionName = actionNameVar.getValue();
		}

		return "selectdoc";
	}
	
	@RequestMapping("/selectaction/{refid}/{actionId}")
	public String selectaction(
			@ModelAttribute@PathVariable("refid")long workflowId,
			@ModelAttribute@PathVariable("actionId")int actionId,
			@ModelAttribute ActionItem actionItem,
			@ModelAttribute List<ActionItem> actions
			) throws Exception {
		Map<String, Object> inputs = Maps.newHashMap();
		inputs.put("entryId", workflowId);
		inputs.put("actionId", actionId);
		actionItem = workflow.getActionItemById(actionId, workflowId, inputs);
		actions = workflow.getActionItem(workflowId, inputs);
		return "selectaction";
	}

	
	@RequestMapping("/rollback/{refid}")
	public  void rollback(@PathVariable("refid")long workflowId) throws Exception {
		workflowService.rollbackRunner(workflowId);
		
	}

	@RequestMapping("/complete/{refid}")
	public void complete(@PathVariable("refid")long workflowId) throws Exception {

		workflowService.completeRunner(workflowId);

		
	}
	@RequestMapping("/delete/{refid}")
	public void delete(@PathVariable("refid")long workflowId) throws Exception {

		workflowService.deleteRunner(workflowId);

	
	}	

}
